package com.drinkdelivery.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drinkdelivery.common.constants.ProductConstant;
import com.drinkdelivery.common.domain.*;
import com.drinkdelivery.common.exception.ServiceException;
import com.drinkdelivery.common.response.ProductSpecResponse;
import com.drinkdelivery.common.response.ProductSpecValueResponse;
import com.drinkdelivery.service.IProductSkuSpecValueService;
import com.drinkdelivery.service.IProductSpecValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.drinkdelivery.mapper.ProductSpecMapper;
import com.drinkdelivery.service.IProductSpecService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 商品规格属性Service业务层处理
 *
 * @author lzh
 * @date 2024-11-08
 */
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements IProductSpecService
{
    @Autowired
    private ProductSpecMapper productSpecMapper;

    @Autowired
    private IProductSpecValueService productSpecValueService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private IProductSkuSpecValueService productSkuSpecValueService;



    /**
     * 查询商品规格属性
     *
     * @param id 商品规格属性主键
     * @return 商品规格属性
     */
    @Override
    public ProductSpec selectProductSpecById(Long id)
    {
        //根据id获取未逻辑删除的数据
        LambdaQueryWrapper<ProductSpec> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductSpec::getId,id);
        lqw.eq(ProductSpec::getIsDeleted, 0);
//        lqw.eq(ProductSpec::getMerchantId,1);设置商户id
        ProductSpec productSpec = this.getOne(lqw);

        List<ProductSpecValue> productSpecValueList = productSpecValueService.selectProductSpecValueListBySpecId(productSpec.getId());

        productSpec.setProductSpecValueList(productSpecValueList);
        return productSpec;
    }

    /**
     * 查询商品规格属性列表
     *
     * @param productSpec 商品规格属性
     * @return 商品规格属性
     */
    @Override
    public List<ProductSpec> selectProductSpecList(ProductSpec productSpec)
    {
        //todo
//        productSpec.setMerchantId(1L);设置商户id
        List<ProductSpec> productSpecList = productSpecMapper.selectProductSpecList(productSpec);

        for (ProductSpec productSpec1 :productSpecList){

            List<ProductSpecValue> productSpecValueList = productSpecValueService.selectProductSpecValueListBySpecId(productSpec1.getId());
            productSpec1.setProductSpecValueList(productSpecValueList);
        }
        return productSpecList;
    }

    /**
     * 新增商品规格属性
     *
     * @param productSpec 商品规格属性
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean insertProductSpec(ProductSpec productSpec)
    {
        validatorProductSpecParam(productSpec);
        //todo
        productSpec.setMerchantId(1);//设置商户id
        List<ProductSpecValue> productSpecValueList = productSpec.getProductSpecValueList();

        for (ProductSpecValue productSpecValue:productSpecValueList){
            productSpecValue.setSpecId(productSpec.getId());
        }
        productSpecValueService.saveBatch(productSpecValueList);
        return this.save(productSpec);
    }

    /**
     * 修改商品规格属性
     *
     * @param productSpec 商品规格属性
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean updateProductSpec(ProductSpec productSpec)
    {
        validatorProductSpecParam(productSpec);
        List<ProductSpecValue> productSpecValueList = productSpec.getProductSpecValueList();
        productSpecValueService.updateBatchById(productSpecValueList);
//        return productSpecMapper.updateProductSpec(productSpec);
        return this.updateById(productSpec);
    }

    /**
     * 批量删除商品规格属性
     *
     * @param ids 需要删除的商品规格属性主键
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean deleteProductSpecByIds(Long[] ids)
    {
        //商品规格属性逻辑删除
        LambdaUpdateWrapper<ProductSpec> luw = new LambdaUpdateWrapper<>();
        luw
                //未逻辑删除
                .eq(ProductSpec::getIsDeleted,0)
                //遍历查询ids
                .in(ProductSpec::getId, Arrays.asList(ids))
                //进行逻辑删除
                .set(ProductSpec::getIsDeleted, 1);
       this.update(luw);

        //商品规格属性值逻辑删除
        LambdaUpdateWrapper<ProductSpecValue> luw2 = new LambdaUpdateWrapper<>();
        luw2.in(ProductSpecValue::getSpecId, Arrays.asList(ids));
        luw2.set(ProductSpecValue::getIsDeleted,ProductConstant.DELETE);
       return productSpecValueService.update(luw2);
    }

    /**
     * 删除商品规格属性信息
     *
     * @param id 商品规格属性主键
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean deleteProductSpecById(Long id)
    {
        LambdaUpdateWrapper<ProductSpec> luw = new LambdaUpdateWrapper<>();
        luw.eq(ProductSpec::getId,id);
        luw.set(ProductSpec::getIsDeleted, ProductConstant.DELETE);
        this.update(luw);

        LambdaUpdateWrapper<ProductSpecValue> luw2 = new LambdaUpdateWrapper<>();
        luw2.eq(ProductSpecValue::getSpecId,id);
        luw2.set(ProductSpecValue::getIsDeleted,ProductConstant.DELETE);
        return productSpecValueService.update(luw2);
    }



    /**
     * 参数校验
     *
     * @param productSpec 商品规格属性
     *
     */

    public void validatorProductSpecParam(ProductSpec productSpec){
        if(ObjectUtil.isEmpty(productSpec.getSpecName())){
            throw new ServiceException("请填写商品规格属性名称");
        }
        if(ObjectUtil.isEmpty(productSpec.getProductSpecValueList())){
            throw new ServiceException("请填写商品规格属性值");
        }

    }

    /**
     * 查询商品规格属性列表
     *
     * @param skuId skuId
     * @return 商品规格属性和商品规格属性值的集合
     */
    @Override
    public List<ProductSpecResponse> selectProductSpecListBySkuId(Long skuId) {
        //根据skuId来查询specValueId
        LambdaQueryWrapper<ProductSkuSpecValue> lqw = new LambdaQueryWrapper<>();
        lqw.select(ProductSkuSpecValue::getSpecValueId);
        lqw.eq(ProductSkuSpecValue::getSkuId,skuId);
        lqw.eq(ProductSkuSpecValue::getIsDeleted,ProductConstant.NO_DELETE);
        List<ProductSkuSpecValue> productSkuSpecValueList = productSkuSpecValueService.list(lqw);

        //商品规格属性值id
        List<Long> specValueIdList = productSkuSpecValueList.stream().map(ProductSkuSpecValue::getSpecValueId).collect(Collectors.toList());

        LambdaQueryWrapper<ProductSpecValue> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(ProductSpecValue::getId,specValueIdList);
        lqw2.eq(ProductSpecValue::getIsDeleted,ProductConstant.NO_DELETE);
        //商品规格属性值列表
        List<ProductSpecValue> productSpecValueList = productSpecValueService.list(lqw2);

        //商品规格属性id
        List<Long> specIdList = productSpecValueList.stream().distinct().map(ProductSpecValue::getSpecId).collect(Collectors.toList());

        LambdaQueryWrapper<ProductSpec> lqw3 =new LambdaQueryWrapper<>();
        lqw3.in(ProductSpec::getId,specIdList);
        lqw3.eq(ProductSpec::getIsDeleted,ProductConstant.NO_DELETE);
        //商品规格属性值列表
        List<ProductSpec> productSpecList = this.list(lqw3);


//       for (ProductSpec productSpec:productSpecList){
//
//           for (ProductSpecValue productSpecValue :productSpecValueList)
//           {
//               if (productSpecValue.getSpecId().equals(productSpec.getId()))
//               {
//                   productSpec.getProductSpecValueList().add(productSpecValue);
//               }
//           }
//       }
        //将商品规格属性由ProductSpec转成ProductSpecResponse
        List<ProductSpecResponse> productSpecResponseList = new ArrayList<>();

        for(ProductSpec productSpec :productSpecList){
            ProductSpecResponse productSpecResponse = new ProductSpecResponse();
            BeanUtils.copyProperties(productSpec,productSpecResponse);
            productSpecResponseList.add(productSpecResponse);
        }
        //将商品规格属性值由ProductSpecValue转成ProductSpecValueResponse
        List<ProductSpecValueResponse> productSpecValueResponseList = new ArrayList<>();
        for (ProductSpecValue productSpecValue:productSpecValueList){
            ProductSpecValueResponse productSpecValueResponse = new ProductSpecValueResponse();
            BeanUtils.copyProperties(productSpecValue,productSpecValueResponse);
            productSpecValueResponseList.add(productSpecValueResponse);
        }


        //将商品规格属性值specId和商品规格属性id进行比较相等,相等则添加到列表，然后赋值给productSpecResponse
        productSpecResponseList.forEach(productSpecResponse -> {
            List<ProductSpecValueResponse> allProductSpecValueResponseList = productSpecValueResponseList.stream().filter(productSpecValueResponse ->
                    productSpecValueResponse.getSpecId().equals(productSpecResponse.getId())).collect(Collectors.toList());

            //设置商品规格属性值
            productSpecResponse.setProductSpecValueResponseList(allProductSpecValueResponseList);
            String productSpecValueString = allProductSpecValueResponseList.stream().map(ProductSpecValueResponse::getValue).collect(Collectors.joining(","));
            productSpecResponse.setSpecNameValue(productSpecValueString);
        });

        return productSpecResponseList;
    }
}
